package com.ruoyi.project.paea.paeaLostVoucher.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.CommonParaTool;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.parawork.domain.PaeaTaskLimitPara;
import com.ruoyi.project.system.parawork.service.IPaeaTaskLimitParaService;
import com.ruoyi.project.tool.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.paea.paeaLostVoucher.mapper.PaeaLostVoucherMapper;
import com.ruoyi.project.paea.paeaLostVoucher.domain.PaeaLostVoucher;
import com.ruoyi.project.paea.paeaLostVoucher.service.IPaeaLostVoucherService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 缺票登记信息Service业务层处理
 *
 * @author ruoyi
 * @date 2020-04-13
 */
@Service
public class PaeaLostVoucherServiceImpl extends ServiceImpl<PaeaLostVoucherMapper, PaeaLostVoucher> implements IPaeaLostVoucherService
{
    @Autowired
    private PaeaLostVoucherMapper paeaLostVoucherMapper;
    @Autowired
    private IPaeaTaskLimitParaService paeaTaskLimitParaService;
    /**
     * 查询缺票登记信息
     *
     * @param id 缺票登记信息ID
     * @return 缺票登记信息
     */
    @Override
    public PaeaLostVoucher selectPaeaLostVoucherById(String id)
    {
        return paeaLostVoucherMapper.selectPaeaLostVoucherById(id);
    }

    /**
     * 查询缺票登记信息列表
     *
     * @param paeaLostVoucher 缺票登记信息
     * @return 缺票登记信息
     */
    @Override
    public List<PaeaLostVoucher> selectPaeaLostVoucherList(PaeaLostVoucher paeaLostVoucher)
    {
        return paeaLostVoucherMapper.selectPaeaLostVoucherList(paeaLostVoucher);
    }

    /**
     * 新增缺票登记信息
     *
     * @param paeaLostVoucher 缺票登记信息
     * @return 结果
     */
    @Override
    public int insertPaeaLostVoucher(PaeaLostVoucher paeaLostVoucher) {
        String code = paeaLostVoucher.getOrgCode() + paeaLostVoucher.getTellerCode() + paeaLostVoucher.getVchNo();
        java.sql.Date limitDate = new java.sql.Date(System.currentTimeMillis());
        paeaLostVoucher.setId(code);
        paeaLostVoucher.setCode(code);
        paeaLostVoucher.setCreator(ShiroUtils.getUserId());
        paeaLostVoucher.setCreateDate(new Date());
        paeaLostVoucher.setStatus(0);
        PaeaTaskLimitPara paeaTaskLimitPara = new PaeaTaskLimitPara();
        paeaTaskLimitPara.setCode("lost_voucher");
        List<PaeaTaskLimitPara> limitParas = paeaTaskLimitParaService.selectPaeaTaskLimitParaList(paeaTaskLimitPara);
        java.sql.Date limitTime = null;
        if (limitParas != null && limitParas.size()>0){
            PaeaTaskLimitPara limitPara = limitParas.get(0);
            Integer limit = limitPara.getLimit();
            //计算期限天数内，有几个节假日
            int hoilNum = 0;
            for(int i=0; i<limit; i++){
                if(CommonParaTool.checkIsHoliday(DateTimeUtil.dayMouthAdd(limitDate, i))){
                    hoilNum ++;
                }
            }

            String newDate = DateTimeUtil.dayMouthAdd(limitDate, limit);
            // 是否节假日，true 是, false 否
            int d_num = 0;
            boolean flag = CommonParaTool.checkIsHoliday(newDate);
            while (flag) {
                d_num++;
                newDate = DateTimeUtil.dayMouthAdd(java.sql.Date.valueOf(newDate), d_num);
                flag = CommonParaTool.checkIsHoliday(newDate);
            }
            limitTime = getLastWorkDate(DateTimeUtil.dayMouthAdd(java.sql.Date.valueOf(newDate), hoilNum));
            System.out.println("-----------------------------------------------------------------------"+limit);
        }
        System.out.println("-----------------------------------------------------------------------"+limitTime);
        //整改期限
        paeaLostVoucher.setLimitDate(limitTime);
        //判断是否已经存在
        PaeaLostVoucher lostVoucher = paeaLostVoucherMapper.selectPaeaLostVoucherById(code);
        if (lostVoucher != null) {
            if (paeaLostVoucher.getStatus() !=3){
                paeaLostVoucher.setCreator(ShiroUtils.getUserId());
                paeaLostVoucher.setCreateDate(new Date());
                paeaLostVoucher.setStatus(0);
                paeaLostVoucher.setLimitDate(limitTime);
                return paeaLostVoucherMapper.updatePaeaLostVoucher(paeaLostVoucher);
            }
        }
        return paeaLostVoucherMapper.insertPaeaLostVoucher(paeaLostVoucher);

    }
    //得到最后的工作日
    public java.sql.Date getLastWorkDate(String date){

        int d_num = 0;
        String workDate = date;
        boolean flag = CommonParaTool.checkIsHoliday(workDate);
        while (flag) {
            d_num++;
            workDate = DateTimeUtil.dayMouthAdd(java.sql.Date.valueOf(workDate), d_num);
            flag = CommonParaTool.checkIsHoliday(workDate);
        }
        return java.sql.Date.valueOf(workDate);

    }

    /**
     * 修改缺票登记信息
     *
     * @param paeaLostVoucher 缺票登记信息
     * @return 结果
     */
    @Override
    public int updatePaeaLostVoucher(PaeaLostVoucher paeaLostVoucher)
    {

        return paeaLostVoucherMapper.updatePaeaLostVoucher(paeaLostVoucher);
    }

    /**
     * 删除缺票登记信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaeaLostVoucherByIds(String ids)
    {
        return paeaLostVoucherMapper.deletePaeaLostVoucherByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除缺票登记信息信息
     *
     * @param id 缺票登记信息ID
     * @return 结果
     */
    @Override
    public int deletePaeaLostVoucherById(String id)
    {
        return paeaLostVoucherMapper.deletePaeaLostVoucherById(id);
    }

    @Override
    public int saveCenterConfirm(List<String> list) {
        List<PaeaLostVoucher> lostVoucherList = new ArrayList<PaeaLostVoucher>();
        for (int i=0;i<=list.size()-1;i++){
            PaeaLostVoucher paeaLostVoucher = paeaLostVoucherMapper.selectPaeaLostVoucherById(list.get(i));
            paeaLostVoucher.setStatus(2);
            paeaLostVoucher.setZxConfirmDate(new Date());
            paeaLostVoucher.setZxConformer(ShiroUtils.getUserId());
            lostVoucherList.add(paeaLostVoucher);
        }
        return paeaLostVoucherMapper.saveCenterConfirm(lostVoucherList);
    }

    @Override
    public int cancel(List<String> list) {
        List<PaeaLostVoucher> lostVoucherList = new ArrayList<PaeaLostVoucher>();
        for (int i=0;i<=list.size()-1;i++){
            PaeaLostVoucher paeaLostVoucher = paeaLostVoucherMapper.selectPaeaLostVoucherById(list.get(i));
            paeaLostVoucher.setStatus(3);
            lostVoucherList.add(paeaLostVoucher);
        }
        return paeaLostVoucherMapper.cancel(lostVoucherList);
    }

}
